﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Library
{
    class DalSanPham
    {
        internal static List<SanPham> GetList()
        {
            List<SanPham> result = new List<SanPham>();

            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                result = da.SanPham.ToList();
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        internal static List<SanPham> GetListByLoaiSPandMTT(String LoaiSP, String MonTT, List<SanPham> lst)
        {
            List<SanPham> result = new List<SanPham>();

            QLDCTTEntities da = new QLDCTTEntities();

            try
            {
                if (LoaiSP != "" && MonTT != "")
                {
                    result = (from sp in lst
                              where sp.MaLoai == LoaiSP
                              && sp.MaMTT == MonTT
                              select sp).ToList();
                }

                else if (LoaiSP == "" && MonTT != "")
                {
                    result = (from sp in lst
                              where sp.MaMTT == MonTT
                              select sp).ToList();
                }

                else if (LoaiSP != "" && MonTT == "")
                {
                    result = (from sp in lst
                              where sp.MaLoai == LoaiSP
                              select sp).ToList();
                }

                else if (LoaiSP == "" && MonTT == "")
                {
                    result = (from sp in lst
                              select sp).ToList();
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return result;
        }

        internal static int Add(SanPham bizsp)
        {
            try
            {
                int result = bizsp.ID;

                SanPham sp = new SanPham();
                sp.MaSP = bizsp.MaSP;
                sp.TenSP = bizsp.TenSP;
                sp.TenHSX = bizsp.TenHSX;
                sp.MaLoai = bizsp.MaLoai;
                sp.MaMTT = bizsp.MaMTT;
                sp.DonGia = bizsp.DonGia;
                sp.HinhAnh = bizsp.HinhAnh;
                sp.MauSac = bizsp.MauSac;
                sp.Size = bizsp.Size;

                QLDCTTEntities entities = new QLDCTTEntities();
                entities.SanPham.Add(sp);

                entities.SaveChanges();

                result = sp.ID;

                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        internal static void Update(SanPham bizsp)
        {
            try
            {
                QLDCTTEntities entities = new QLDCTTEntities();
                SanPham sp = entities.SanPham.Single(p => p.ID == bizsp.ID);

                sp.MaSP = bizsp.MaSP;
                sp.TenSP = bizsp.TenSP;
                sp.TenHSX = bizsp.TenHSX;
                sp.MaLoai = bizsp.LoaiSanPham.MaLoai;
                sp.MaMTT = bizsp.MonTheThao.MaMTT;
                sp.DonGia = bizsp.DonGia;
                sp.HinhAnh = bizsp.HinhAnh;
                sp.MauSac = bizsp.MauSac;
                sp.Size = bizsp.Size;

                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        internal static void Delete(SanPham bizsp)
        {
            try
            {
                QLDCTTEntities entities = new QLDCTTEntities();
                SanPham sp = entities.SanPham.Single(p => p.ID == bizsp.ID);

                entities.SanPham.Remove(sp);

                entities.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
